package com.example.xsssglxt.controller;

import com.alibaba.excel.EasyExcel;
import com.example.xsssglxt.entity.Louyu;
import com.example.xsssglxt.entity.Student;
import com.example.xsssglxt.mapper.LouyuMapper;
import com.example.xsssglxt.service.LouyuService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Controller
@RequestMapping("/louyu")
public class LouyuController {
    @Autowired
    private LouyuMapper louyuMapper;
  @Autowired
  private LouyuService louyuService;
    @GetMapping("/index")
    public Map<String,Object> index(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String name){
pageNum=(pageNum-1)*pageSize;
      List<Louyu> data=louyuMapper.selectpage(pageNum,pageSize,name);
      Integer total=louyuMapper.selecttotal(name);
      System.out.println(data);
      Map<String,Object> res=new HashMap<>();
      res.put("data",data);
      res.put("total",total);
      return res;
  }

  @GetMapping("/xl")
  public Map<String,Object> xl(){
        List<Louyu> data=louyuMapper.selectlist();
        Map<String,Object> res=new HashMap<>();
        res.put("data",data);
        return res;
  }
  @PostMapping("/add")
    public Map<String,Object> addly(@RequestBody Louyu louyu){
      Map<String,Object> res=new HashMap<>();
      if(louyuService.louyu(louyu) == 1){
          res.put("code",200);
          res.put("msg","操作成功");
      }
      return res;

  }
  @PostMapping("/update")
    public Map<String ,Object> xgly(@RequestBody Louyu louyu){
        Map<String,Object> res=new HashMap<>();
        if(louyuService.louyuxg(louyu) == 1){
            res.put("code",200);
            res.put("msg","操作成功");

        }
        return  res;
  }
  @DeleteMapping("/{id}")
    public Map<String,Object> scly(@PathVariable Integer id){
        Map<String,Object> res=new HashMap<>();
        Integer col=louyuMapper.deleter(id);
        res.put("data",col);
        return res;

  }
    @GetMapping("/down")
    public Result down(HttpServletResponse response) throws IOException {
        List<Louyu> studentList=louyuMapper.selectlist();
        response.setContentType("application/octet-stream");
        String fileName= URLEncoder.encode("楼宇表","UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(),Louyu.class).autoCloseStream(Boolean.FALSE).sheet("楼宇表").doWrite(studentList);


        return null;
    }

}
